<template>
    <div class="top">
        <div class="top-container">
            <!-- 头部 -->
            <header>
                <div>
                    <div class="nav">
                        <!-- 导航栏 -->
                        <div class="header-nav">
                            <div class="nav-container">
                                <ul class="dropdown">
                                    <li><img src="https://course.myhope365.com/img/logo-text-green.2137bfaa.png" alt=""></li>
                                    <li><router-link to="/" class="data" style="color: black;">首页</router-link></li>
                                    <li>
                                        <div class="data btn-primary" @click="toggleUl">课程分类
                                            <i class="action">
                                                <img src="@/assets/310向下、展开 (1).png" alt="">
                                            </i>
                                            <div id="cSelect1">
                                                <ul v-if="isUlVisible">
                                                    <li v-for="item in cList1" :key="item.subjectId">
                                                        <div @click="goToSearch(item.subjectId)">{{ item.subjectTitle }}</div>
                                                    </li>
                                                </ul>
                                            </div>
                                        </div>
                                    </li>
                                    <li><div class="data">教学管理平台</div></li>
                                </ul>
                            </div>
                        </div>
                        <!-- 搜索框 -->
                        <span class="search">
                            <span>
                                <input v-model="keyword" type="text" placeholder="搜索课程~">
                                <span @click="goSearch">
                                    <button @click="handleSearch">
                                        <img src="" alt="">
                                    </button>
                                </span> 
                            </span>
                        </span>
                        <!-- 登录注册 -->
                        <div class="login">
                            <div class="login-box">
                                <!-- 购物车 -->
                                <router-link to="/shop" class="white-box">
                                    <span>
                                        <img src="" alt="">
                                    </span>
                                </router-link>
                            </div>
                            <div class="user">
                                <loginView>登录/注册</loginView>
                            </div>
                        </div>
                    </div>
                </div>
            </header>
        </div>
    </div>
</template>
<script>
    import loginView from "../views/login/loginView.vue"
    import { getDetail } from "../api/request"
    export default {
        data() {
            return {
                isUlVisible: false,
                cList1: [],
                keyword: ''
            };
        },
        props: {
            // 接收从 searchView 传递的 keyword
            initKeyword: {
                type: String,
                default: ''
            }
        },
        watch: {
            initKeyword(newVal) {
                // 监听 initKeyword 变化，更新输入框的值
                this.keyword = newVal;
            }
        },
        components: {
            loginView
        },
        methods: {
            toggleUl() {
                this.isUlVisible = !this.isUlVisible;
            },
            goSearch() {
                console.log(111);
                this.$router.push({
                    path: '/search',
                    query: {
                        keyword: this.keyword
                    }
                }).catch(err => {
                    // 忽略导航重复错误
                    if (err.name !== 'NavigationDuplicated') {
                        throw err;
                    }
                });
            },
            handleSearch() {
                console.log('搜索关键字:', this.keyword);
                // 触发自定义事件，传递 keyword
                this.$emit('search', this.keyword);
            },
            // 新增跳转方法，传递 subjectId
            goToSearch(subjectId) {
                this.$router.push({
                    path: '/search',
                    query: {
                        subjectId: subjectId
                    }
                }).catch(err => {
                    if (err.name !== 'NavigationDuplicated') {
                        throw err;
                    }
                });
            }
        },
        beforeCreate(){},
        created(){
            this.keyword = this.initKeyword;
            getDetail().then(res => {
                console.log(res.rows);
                this.cList1 = res.rows;
            })
        },
        beforeMount(){},
        mounted(){},
        beforeUpdate(){},
        updated(){},
        beforeDestroy(){},
    };
</script>
<style lang="scss" scoped>
* {
    margin: 0;
    padding: 0;
}
ol,ul,li {
    list-style: none!important;
}
a {
    text-decoration: none!important;
    :hover {
        color: #00cf8c;
    }
}
img {
    border: 0;
    vertical-align: middle
}
.top {
    width: 100%;
    height: 80px;
    background: #fff;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;
}
.dropdown {
    >li {
        cursor: pointer;
    }
}
header {
    width: 100%;
    // height: 80px;
    background: #fff;
    background-color: transparent !important;
    color: #fff;
    >div  {
        margin: 0 auto;
        width: 1160px;
        .nav {
            height: 80px;
            display: flex;
            align-items: center;
            /* 左边导航 */
            .header-nav {
                margin-right: 30px;
                margin-left: 40px;
                .nav-container {
                    color: black;
                    >ul>li {
                        float: left;
                        margin: 0 25px 0 0;
                        font-size: 16px;
                        position: relative;
                        color: black;
                        font-size: 18px;
                        font-weight: 600;
                        
                    }
                    #cSelect1 {
                        position: absolute;
                        top: 100%;
                        left: 0;
                        z-index: 1000;
                        background-color: #fff;
                        // border: 1px solid #d9d9d9;
                        border-radius: 4px;
                        box-shadow: 0 2px 8px rgba(0, 0, 0,.15);
                        min-width: 160px;
                        // padding: 5px 0;
                        >ul {
                            margin: 0;
                            padding: 0;
                            list-style: none;
                            >li {
                                clear: both;
                                margin: 0;
                                padding: 8px 15px; 
                                &:hover {
                                    background-color: #f5f5f5;
                                }
                                >div {
                                    color: rgba(0, 0, 0,.65);
                                    font-weight: 400;
                                    font-size: 14px; 
                                    display: block;
                                }
                            } 
                        }
                    }
                }
            }
            >.search {
                width: 300px;
                display: inline-block;
                text-align: start;
                vertical-align: top;
                margin-left: auto;
                >span {
                    box-sizing: border-box;
                    color: rgba(0, 0, 0, .65);
                    font-size: 14px;
                    line-height: 1.5;
                    list-style: none;
                    position: relative;
                    display: table;
                    width: 100%;
                    border-collapse: separate;
                    border-spacing: 0;
                    >input {
                        height: 38px;
                        border-radius: 20px;
                        border-top-right-radius: 0;
                        border-bottom-right-radius: 0;
                        float: left;
                        width: 85%;
                        margin-bottom: 0;
                        padding: 6px 11px;
                        font-size: 16px;
                        box-sizing: border-box;
                        margin: 0;
                        line-height: 1.5;
                        background-color: #fff;
                        background-image: none;
                        border: 1px solid #d9d9d9;
                    }
                    >span {
                        position: relative;
                        display: inline-block;
                        box-sizing: border-box;
                        background-color: #00cf8c;
                        height: 38px;
                        padding: 0 11px;
                        color: rgba(0, 0, 0, .65);
                        border: 1px solid #d9d9d9;
                        border-radius: 20px;
                        border-top-left-radius: 0;
                        border-bottom-left-radius: 0;
                        >button {
                            height: 33px;
                            margin-left: -2px;
                            position: relative;
                            display: inline-block;
                            background-color: transparent;
                            text-align: center;
                            border-color: #d9d9d9;
                            border: 1px solid transparent;
                            // border-color: #d9d9d9;
                        }
                    }
                }
            }
            >.login {
                height: 100%;
                font-size: 14px;
                display: flex;
                align-items: center;
                // 购物车
                >.login-box {
                    .white-box {
                        width: 36px;
                        height: 36px;
                        background: #fff;
                        box-shadow: 0 2px 3px 0 rgba(0, 0, 0, .23);
                        border-radius: 50%;
                        margin: 0 20px;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        >span {
                            margin: 0;
                            padding: 0;
                            box-sizing: border-box;
                            color: rgba(0, 0, 0,.65);
                            font-size: 14px;
                            line-height: 1;
                            list-style: none;
                            position: relative;
                            >img {
                                vertical-align: middle;
                                width: 20px;
                            }
                        }
                    }
                }
                // 登录注册
                >.user {
                    display: flex;
                    align-items: center;
                    position: relative;
                    justify-content: center;
                    
                    //登录注册按钮
                    .register1 {
                        width: 94px;
                        height: 30px;
                        background: #fff;
                        box-shadow: 0 2px 3px 0 rgba(0, 0, 0, .23);
                        color: #00cf8c;
                        border-radius: 15px;
                        font-weight: 600;
                        font-size: 15px;
                        text-align: center;
                    }
                }
            }
        }
    }
}
</style>